package com.young.core.domain;

import java.io.Serializable;
import java.util.Date;

/**
 * 锁，Broker直接通过lock决胜角色
 * 锁的粒度，由namespace控制，即一个namespace只包含一个持有锁的owner
 *
 * 针对当前业务而言，owner即为broker的ip地址
 */
public class LockDO implements Serializable {

    private Integer id;
    /**
     * 锁名称
     */
    private String namespace;

    /**
     * 锁持有者
     */
    private String owner;

    /**
     * 逻辑字段，剩余时间
     * 此值由当前时间 与 上一次keepAlive更新时间的差值
     * 用来判断，lock是否尚在续约期间
     */
    private transient long remaining;

    /**
     * 乐观锁版本号
     */
    private long version;

    private Date createTime;

    /**
     * 锁保活的时间戳
     */
    private Date updateTime;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNamespace() {
        return namespace;
    }

    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public long getRemaining() {
        return remaining;
    }

    public void setRemaining(long remaining) {
        this.remaining = remaining;
    }

    public long getVersion() {
        return version;
    }

    public void setVersion(long version) {
        this.version = version;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    /**
     * 逻辑方法，是否过期
     * @param ttl
     * @return
     */
    public boolean isExpired(int ttl) {
        return remaining - ttl > 0 || owner == null;
    }
}
